//
//  GenerateRoseViewController.m
//  ROSE
//
//  Created by 艾阳 on 2018/5/31.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "GenerateRoseViewController.h"
#import "GenerateRoseCell.h"
#import "SureGenerateViewController.h"

@interface GenerateRoseViewController ()<LMJNavUIBaseViewControllerDataSource,LMJElementsFlowLayoutDelegate>

@property (nonatomic,strong) NSMutableArray *roseListArray;

/**
 当前选中的cell
 */
@property (nonatomic,strong) GenerateRoseCell *currentSelectCell;

@end

@implementation GenerateRoseViewController

- (NSMutableArray *)roseListArray{
    if (!_roseListArray) {
        _roseListArray = [[NSMutableArray alloc]init];
        
    }
    return _roseListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:240/255.0 blue:241/255.0 alpha:1];
    self.collectionView.backgroundColor = [UIColor colorWithRed:239/255.0 green:240/255.0 blue:241/255.0 alpha:1];
    self.title = @"生成Rose";
    [self.collectionView registerClass:[GenerateRoseCell class] forCellWithReuseIdentifier:NSStringFromClass([GenerateRoseCell class])];
    [self getRoseList];
    [self setGenerateBtn];
}
#pragma mark - setUI

- (void)setGenerateBtn{
    UIButton *generateBtn = [[UIButton alloc]init];
    generateBtn.backgroundColor = [UIColor themeColor];
    generateBtn.titleLabel.textColor = [UIColor whiteColor];
    [generateBtn setTitle:@"确认兑换" forState:UIControlStateNormal];
    [generateBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    generateBtn.layer.masksToBounds = YES;
    generateBtn.layer.cornerRadius = 20.0f;
    [self.view addSubview:generateBtn];
    [generateBtn addTarget:self action:@selector(sureGenerate) forControlEvents:UIControlEventTouchUpInside];
    
    [generateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(-35);
        make.bottom.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
}

/**
 确认兑换
 */
- (void)sureGenerate{
    if (self.currentSelectCell==nil) {
        /**必须选中一种*/
        NSLog(@"please select a rose");
    }else{
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:self.currentSelectCell];
        GenerateModel *currentModel = self.roseListArray[indexPath.row];
        if (currentModel.SY.integerValue>0) {
            SureGenerateViewController *sureGenerateVc = [[SureGenerateViewController alloc]init];
            [self.navigationController pushViewController:sureGenerateVc animated:YES];
        }else{
            /**库存不足 无法兑换*/
        }
    }
}

#pragma mark - DataRequest

- (void)getRoseList{
    [[LMJRequestManager sharedManager] POST:[NSString stringWithFormat:@"%@/rose/mgkc",url_base] parameters:nil completion:^(LMJBaseResponse *response) {
        if (response.error==NULL&&[response.responseObject[@"errorcode"] isEqualToString:@"000000"]) {
            [self dataConvert:response.responseObject[@"dataobj"]];
            [self.collectionView reloadData];
        }
    }];
}

- (void)dataConvert:(NSArray *)listArray{
    for (NSDictionary * roseDetailDict in listArray) {
        GenerateModel *roseModel = [[GenerateModel alloc]init];
        [roseModel setValuesForKeysWithDictionary:roseDetailDict];
        [self.roseListArray addObject:roseModel];
    }
}

#pragma mark - CollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.roseListArray.count==0?0:self.roseListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GenerateRoseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([GenerateRoseCell class]) forIndexPath:indexPath];
    [cell setDataWithGenerateModel: self.roseListArray[indexPath.row]];
    return cell;
}


/**
 选中效果
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GenerateRoseCell *cell =(GenerateRoseCell*) [collectionView cellForItemAtIndexPath:indexPath];
    if (cell.isSelected) {
        /**当前cell已经被选中
         取消选中样式，把cSC置为空
         */
        [self cancelSelectStyle:cell];
        self.currentSelectCell = nil;
    }else{
        /**当前cell未被选中
         1.选中当前cell
         2.如果cSC不为空，取消cSC的选中样式,
         3.把cSC设置为当前cell
         */
        [self setSelectStyle:cell];
        if (self.currentSelectCell != NULL) {
            [self cancelSelectStyle:self.currentSelectCell];
            self.currentSelectCell = cell;
        }else{
            self.currentSelectCell = cell;
        }
    }
}

/**
 设置选中样式
 */
- (void)setSelectStyle:(GenerateRoseCell *)cell{
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.borderWidth =1.0f;
    cell.layer.borderColor = [UIColor themeColor].CGColor;
    cell.isSelected = YES;
}

/**
 取消选中样式
 */
- (void)cancelSelectStyle:(GenerateRoseCell *)cell{
    cell.backgroundColor = [self viewThemeColor];
    cell.layer.borderWidth =0;
    cell.isSelected = NO;
}

#pragma mark - CollectionViewFlowLayout

/**
 Cell-Size
 */
- (CGSize)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((size_width-85)/2, (size_width-85)/2*505.0/555.0+10);
}


/**
 collectionview距离边界距离
 */
- (UIEdgeInsets)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout edgeInsetsInCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(20, 35, 10, 35);
}


/**
 行间
 */
- (CGFloat)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView columnsMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 15;
}
#pragma mark - CollectionViewControllerDataSource

- (UICollectionViewLayout *)collectionViewController:(LMJCollectionViewController *)collectionViewController layoutForCollectionView:(UICollectionView *)collectionView{
    
    LMJElementsFlowLayout * elementsFlowLayout = [[LMJElementsFlowLayout alloc] initWithDelegate:self];
    return elementsFlowLayout;
    
}

#pragma mark - naviBar
- (BOOL)navUIBaseViewControllerIsNeedNavBar:(LMJNavUIBaseViewController *)navUIBaseViewController{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIColor*)viewThemeColor{
    return [UIColor colorWithRed:239/255.0 green:240/255.0 blue:241/255.0 alpha:1];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
